package ruanjian.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ruanjian.domain.Customer;
import ruanjian.service.CustomerService;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    // 依赖注入
    @Autowired
    private CustomerService customerService;

    /**
     *  客户列表
     */
    @RequestMapping(value = "/list.action")
    public String list(Model model) {
        // 条件查询所有客户

        List<Customer> customers = customerService.selectCustomerList();
        model.addAttribute("page", customers);
        return "customer";
    }
    @RequestMapping("/create.action")
    @ResponseBody
    public String customerCreate(Customer customer) {

        // 执行Service层中的创建方法，返回的是受影响的行数
        int rows = customerService.createCustomer(customer);
        if(rows > 0){
            return "OK";
        }else{
            return "FAIL";
        }
    }

    /**
     * 通过id获取客户信息
     */
    @RequestMapping("/getCustomerById.action")
    @ResponseBody
    public Customer getCustomerById(Integer id) {
        Customer customer = customerService.getCustomerById(id);
        return customer;
    }

    /**
     * 更新客户
     */
    @RequestMapping("/update.action")
    @ResponseBody
    public String customerUpdate(Customer customer) {
        int rows = customerService.updateCustomer(customer);
        if(rows > 0){
            return "OK";
        }else{
            return "FAIL";
        }
    }
    /**
     * 删除客户
     */
    @RequestMapping("/delete.action")
    @ResponseBody
    public String customerDelete(Integer id) {
        int rows = customerService.deleteCustomer(id);
        if(rows > 0){
            return "OK";
        }else{
            return "FAIL";
        }
    }
}
